package Controls;

import Core.Control;
import Core.LayoutManager;
import java.util.Enumeration;
import java.util.Vector;

public class RadioGroup extends LayoutManager
{
	int radios=0;

	public RadioGroup(LayoutManager parent)
	{
		super(parent.getAvailableW(), 0);

		parentManager = parent;
		allowToEnlarge = true;
		type = VERTICAL;
		scrollEnable = true;
		h_border = v_border = spacing = 0;
	}

	public void updateDimensions()
	{
		super.updateDimensions();
		if(H > parentManager.getAvailableH()) H = parentManager.getAvailableH();
	}

	public RadioButton addRadioButton(RadioButton r)
	{
		if(r == null) return null;

		r.setGroup(this);
		addControl(r);
		radios++;

		return r;
	}

	public RadioButton addRadioButton(String caption)
	{
		RadioButton r = new RadioButton(caption, getAvailableW());
		r.setGroup(this);
		addControl(r);
		return r;
	}

	public RadioButton removeRadioButton(RadioButton r)
	{
		if(removeControl(r) != null)
		{
			radios--;
			return r;
		}

		return null;
	}

	public void update(RadioButton r)
	{
		for(Enumeration i = controls.elements(); i.hasMoreElements(); )
		{
			RadioButton button = (RadioButton) i.nextElement();
			if(button != r) button.setCheck(false);
		}
	}

	public Control addControl(Control c)
	{
		if(c instanceof RadioButton) return super.addControl(c);
		return null;
	}
	
	public void setSelect(int number)
	{
		RadioButton r = (RadioButton) controls.elementAt(number);
		r.setCheck(true);
		update(r);
	}
	
	public void setSelect(RadioButton r)
	{
		if(r != null && controls.contains(r))
		{
			r.setCheck(true);
			update(r);
		}
	}

	public int getSelect(int def)
	{
		int res=0;
		
		for(Enumeration i = controls.elements(); i.hasMoreElements(); )
		{
			RadioButton button = (RadioButton) i.nextElement();
			if(button.getChecked()) return res;
			else res++;
		}

		return def;
	}
}
